Calcifer Calcifer 2
Linux

监控 cpu使用率

2016/03/31 13:17 6151 次阅读 王梓
打赏
✸ ✸ ✸

示例:点击 -> 性能监控

[root@wangzi go]# cat /proc/stat
cpu  25187586 4339 20108620 1703341684 3875717 0 58452 0 0 0

pytho代码:

#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com
def cpu_use():
    use=0
    with open('/proc/stat','r') as file:
        for line in file.readlines():
            if line.startswith('cpu ',0,4):
                use=round((1-float(line.split()[4])/sum([int(x) for x in line.split()[1:]]))*100,5)
    insert_sql="""insert into `monitor_cpu_use` (`use`,`flow_time`) values ({0},'{1}')""".format(use,now_zero)
    cursorUpdate(insert_sql,[])
✸ ✸ ✸

📜 版权声明

本文作者:王梓 | 原文链接:https://www.bthlt.com/note/9565314-Linux监控 cpu使用率

出处:葫芦的运维日志 | 转载请注明出处并保留原文链接

📜 留言板

留言提交后需管理员审核通过才会显示